﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Model;
using SystemFrameWork.Enums;
using SystemFrameWork;

namespace DutyWeb.Admin.System
{
    public partial class SysUserGroupManagement : BasePage
    {
        /// <summary>
        /// edit flag of this page
        /// </summary>
        private bool editFlag
        {
            get
            {
                if (this.LabEditFlag.Text.ToLower() == ConstantsManager.True)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.LabEditFlag.Text = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.initialize();
            }
            this.LeftMenuTree2.DisplayCheckBox = true;
        }

        private void initialize()
        {
            string id = Request.QueryString["id"];
            if (StringHelper.IsGUID(id))
            {
                this.editFlag = true;
                this.LabHidID.Text = id;
                RoleView ugv = new RoleView();
                ugv.ID = new Guid(id);
                List<RoleView> list = base.sys_RoleManagement.LoadUserGroupByCondition(ugv);
                if (list.Count() > 0)
                {
                    this.txtUserGroupName.Text = list[0].RoleName.ToString();
                    this.DepartmentTreeUC.SelectedDepartmentName = list[0].DepartmentName;
                    this.DepartmentTreeUC.SelectedDepartmentID = list[0].DepartmentID;

                    this.LeftMenuTree2.SelectedFunctionModule = base.sys_RoleManagement.LoadFunctionModuelID(ugv.ID);
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.editFlag)
            {
                this.UpdateuserGroup();
            }
            else
            {
                this.InsertUserGroup();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void InsertUserGroup()
        {
            if (string.IsNullOrEmpty(this.txtUserGroupName.Text.Trim()))
            {
                base.ShowMessage("用户组名称不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (this.DepartmentTreeUC.SelectedDepartmentID == null)
            {
                base.ShowMessage("关联部门不能为空！", ExceptionLevel.Warn);
                return;
            }

            List<Guid?> list = this.LeftMenuTree2.SelectedFunctionModule;
            if (list == null)
            {
                base.ShowMessage("功能模块不能为空!", ExceptionLevel.Warn);
                return;
            }

            Sys_Role group = base.sys_RoleManagement.LoadUserGroupByName(this.txtUserGroupName.Text.Trim());
            if (group != null)
            {
                base.ShowMessage("用户组不能重名！", ExceptionLevel.Warn);
                return;
            }

            Sys_Role usergroup = new Sys_Role();
            usergroup.ID = Guid.NewGuid();
            usergroup.Name = this.txtUserGroupName.Text.Trim();
            usergroup.CreatedBy = new Guid(HttpContext.Current.User.Identity.Name);
            usergroup.ModifiedBy = usergroup.CreatedBy;
            usergroup.CreatedOn = DateTime.Now;
            usergroup.ModifiedOn = DateTime.Now;
            base.sys_RoleManagement.Insert(usergroup);

            foreach (Guid mID in list)
            {
                Sys_FunctionModuleSys_Role functionMUserGroup = new Sys_FunctionModuleSys_Role();
                functionMUserGroup.ID = Guid.NewGuid();
                functionMUserGroup.CreatedBy = usergroup.CreatedBy;
                functionMUserGroup.ModifiedBy = usergroup.CreatedBy;
                functionMUserGroup.ModifiedOn = DateTime.Now;
                functionMUserGroup.CreatedOn = DateTime.Now;
                functionMUserGroup.Sys_FunctionModuleID = mID;
                functionMUserGroup.Sys_RoleID = usergroup.ID;
                base.sys_RoleManagement.Insert(functionMUserGroup);
            }

            Sys_DepartmentRole departmentGroup = new Sys_DepartmentRole();
            departmentGroup.ID = Guid.NewGuid();
            departmentGroup.ModifiedBy = usergroup.CreatedBy;
            departmentGroup.CreatedBy = usergroup.CreatedBy;
            departmentGroup.CreatedOn = DateTime.Now;
            departmentGroup.ModifiedOn = DateTime.Now;
            departmentGroup.DepartmentID = this.DepartmentTreeUC.SelectedDepartmentID;
            departmentGroup.RoleID = usergroup.ID;
            base.sys_RoleManagement.Insert(departmentGroup);

            //commit the transaction
            base.sys_RoleManagement.UpdateChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateuserGroup()
        {
            if (string.IsNullOrEmpty(this.txtUserGroupName.Text.Trim()))
            {
                base.ShowMessage("用户组名称不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (this.DepartmentTreeUC.SelectedDepartmentID == null)
            {
                base.ShowMessage("关联部门不能为空！", ExceptionLevel.Warn);
                return;
            }

            List<Guid?> list = this.LeftMenuTree2.SelectedFunctionModule;
            if (list == null)
            {
                base.ShowMessage("功能模块不能为空!", ExceptionLevel.Warn);
                return;
            }

            Sys_Role group = base.sys_RoleManagement.LoadUserGroupByID(new Guid(this.LabHidID.Text));
            group.Name = this.txtUserGroupName.Text.Trim();

            Sys_Role group1 = base.sys_RoleManagement.LoadUserGroupByName(this.txtUserGroupName.Text.Trim());
            if (group1 != null && group.ID != group1.ID)
            {
                base.ShowMessage("用户组不能重名！", ExceptionLevel.Warn);
                return;
            }

            group.ModifiedBy = base.CurrentUserID;
            group.ModifiedOn = DateTime.Now;
            List<Sys_DepartmentRole> listD = base.sys_RoleManagement.LoadSys_DepartmentSys_Role(group.ID);
            base.sys_RoleManagement.DeleteSys_DepartmentRole(listD);
            List<Sys_FunctionModuleSys_Role> listF = base.sys_RoleManagement.Load_SysFunctionModuleSys_Role(group.ID);
            base.sys_RoleManagement.DeleteSys_FunctionModuleSys_UserGroup(listF);

            Sys_DepartmentRole sdg = new Sys_DepartmentRole();
            sdg.ID = Guid.NewGuid();
            sdg.RoleID = group.ID;
            sdg.DepartmentID = this.DepartmentTreeUC.SelectedDepartmentID;
            sdg.CreatedBy = group.ID;
            sdg.CreatedBy = base.CurrentUserID;
            sdg.ModifiedBy = base.CurrentUserID;
            sdg.CreatedOn = DateTime.Now;
            sdg.ModifiedOn = DateTime.Now;
            base.sys_RoleManagement.Insert(sdg);

            foreach (Guid mID in list)
            {
                Sys_FunctionModuleSys_Role functionMUserGroup = new Sys_FunctionModuleSys_Role();
                functionMUserGroup.ID = Guid.NewGuid();
                functionMUserGroup.CreatedBy = group.ModifiedBy;
                functionMUserGroup.ModifiedBy = group.ModifiedBy;
                functionMUserGroup.ModifiedOn = DateTime.Now;
                functionMUserGroup.CreatedOn = DateTime.Now;
                functionMUserGroup.Sys_FunctionModuleID = mID;
                functionMUserGroup.Sys_RoleID = group.ID;
                base.sys_RoleManagement.Insert(functionMUserGroup);
            }

            base.sys_RoleManagement.UpdateChanges();
            base.ShowMessage("操作成功!", ExceptionLevel.Info);
        }
    }
}
